﻿@inherits LayoutComponentBase
@layout MainLayout
@inject NavigationManager Navigator

<div class="bb-title"><img class="bb-icon" src="_content/BootstrapBlazor.Shared/images/logo.png" /><span>基于 Bootstrap 和 Blazor 的企业级组件库</span></div>

<p class="text-center">
    <img alt="star" src="https://gitee.com/LongbowEnterprise/BootstrapBlazor/badge/star.svg?theme=gvp" style="margin-bottom: -1px;" />
    <img alt="nuget" src="https://img.shields.io/nuget/vpre/BootstrapBlazor.svg?logo=nuget&logoColor=green">
    <span class="d-none d-sm-inline">
        <img alt="master" src="https://img.shields.io/github/workflow/status/ArgoZhang/BootstrapBlazor/Package%20to%20Nuget/master?label=master&logo=github&logoColor=green">
        <img alt="download" src="https://img.shields.io/nuget/dt/BootstrapBlazor.svg?logo=nuget&logoColor=green">
        <img alt="license" src="https://img.shields.io/github/license/argozhang/bootstrapblazor.svg?logo=git&logoColor=red">
        <img alt="repo" src="https://img.shields.io/github/repo-size/ArgoZhang/BootstrapBlazor.svg?logo=github&logoColor=green&label=repo">
        <img alt="commit" src="https://img.shields.io/github/last-commit/ArgoZhang/BootstrapBlazor/master.svg?logo=github&logoColor=green&label=commit">
    </span>
</p>

<Tab IsBorderCard="true">
    <TabItem Text="示例" Icon="fa fa-desktop">
        @Body
    </TabItem>
    <TabItem Text="Razor" Icon="fa fa-html5">
        <Pre @key="@RazorFileName" CodeFile="@RazorFileName"></Pre>
    </TabItem>
    <TabItem Text="C#" Icon="fa fa-file-code-o">
        <Pre @key="@CsharpFileName" CodeFile="@CsharpFileName"></Pre>
    </TabItem>
    <TabItem Text="相关视频" Icon="fa fa-file-video-o">
        <Pre @key="@VideoFileName" CodeFile="@VideoFileName"></Pre>
    </TabItem>
</Tab>

@code {
    [NotNull]
    private string? RazorFileName { get; set; }

    [NotNull]
    private string? CsharpFileName { get; set; }

    [NotNull]
    private string? VideoFileName { get; set; }

    private List<KeyValuePair<string, string>> Dictionarys = new();

    protected override void OnInitialized()
    {
        base.OnInitialized();

        LoadDictionary();
    }

    private void LoadDictionary()
    {
        using var res = GetType().Assembly.GetManifestResourceStream($"{GetType().Assembly.GetName().Name}.Pages.Samples.docs.json");

        var config = new ConfigurationBuilder()
            .AddJsonStream(res)
            .Build();
        Dictionarys.AddRange(config.GetChildren().SelectMany(c => new KeyValuePair<string, string>[] { new KeyValuePair<string, string>(c.Key, c.Value) }));
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();

        var page = Navigator.ToBaseRelativePath(Navigator.Uri);
        var comName = page.Split("/").LastOrDefault() ?? string.Empty;
        if (!string.IsNullOrEmpty(comName))
        {
            RazorFileName = Dictionarys.FirstOrDefault(i => i.Key.Contains(comName, StringComparison.OrdinalIgnoreCase)).Value
                ?? CultureInfo.CurrentCulture.TextInfo.ToTitleCase(comName);

            RazorFileName = $"{RazorFileName}.razor";
            CsharpFileName = $"{RazorFileName}.cs";
            VideoFileName = "";
        }
    }
}
